package com.ebeitech.auction.view.homepage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebeitech.auction.R;
import com.ebeitech.auction.bean.Auction;
import com.ebeitech.auction.bean.AuctionBidder;
import com.ebeitech.auction.bean.AuctionPhase;
import com.ebeitech.auction.bean.LandInfo;
import com.ebeitech.auction.constants.CommonConstants;
import com.ebeitech.auction.domain.ChatMessage;
import com.ebeitech.auction.view.auction.AuctionDetailActivity;
import com.ebeitech.library.util.StringUtil;
import com.ebeitech.library.util.TimeUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by alice on 2017/11/1.
 */
public class AuctionAdapter extends BaseAdapter {

	private int mCurrentTab;
	private Context mContext;
	private List<Auction> list;

	public AuctionAdapter(Context context, int currentTab, List<Auction> list) {
		mContext = context;
		mCurrentTab = currentTab;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder mViewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.ebei_view_list_auction_item, null);
			mViewHolder = new ViewHolder();
			mViewHolder.setView(convertView);
			convertView.setTag(mViewHolder);

			mViewHolder.viewTitle.measure(0, 0);
			int height = mViewHolder.viewTitle.getMeasuredHeight();
			ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) mViewHolder.ivFlag.getLayoutParams();
			lp.height = height * 3 / 5;
			mViewHolder.ivFlag.setLayoutParams(lp);
		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}

		final Auction auction = list.get(position);
		LandInfo mLandInfo = auction.getLandInfo();
		if (mLandInfo == null) {
			mLandInfo = new LandInfo();
		}

		if (CommonConstants.STATE_PUBLISHED.equals(auction.getStateCode())) {
			mViewHolder.viewAuctionInfo.setVisibility(View.GONE);
			mViewHolder.viewAddress.setVisibility(View.VISIBLE);
		} else {
			mViewHolder.viewAuctionInfo.setVisibility(View.VISIBLE);
			mViewHolder.viewAddress.setVisibility(View.GONE);
		}

		String content = StringUtil.getDefaultString(auction.getArea());
		if (!StringUtil.isEmpty(content)) {
			content += " | ";
		}
		content += StringUtil.getDefaultString(auction.getBlockName());
		mViewHolder.tvArea.setText(content);
		String startTime = mLandInfo.getStartDate();//auction.getAuctionStartTime();
		String endTime = mLandInfo.getEndDate();//auction.getAuctionEndTime();
		String time = "";
		if (CommonConstants.STATE_PUBLISHED.equals(auction.getStateCode())) {
			time = TimeUtil.getDisplayTime(startTime);
			time = time.replaceAll("-", "/");
			time = mContext.getString(R.string.ebei_begin_with_x, time);
			mViewHolder.tvTime.setTextColor(mContext.getResources().getColor(R.color.ebei_blue));
		} else if (CommonConstants.STATE_BEGINNING.equals(auction.getStateCode())) {
			time = mContext.getString(R.string.ebei_auction_doing);
			mViewHolder.tvTime.setTextColor(mContext.getResources().getColor(R.color.ebei_green));
		} else if (CommonConstants.STATE_FINISHED.equals(auction.getStateCode())) {
			if (StringUtil.isEmpty(endTime)) {
				time = "";
			} else {
				time = TimeUtil.getDateToString(new Date(TimeUtil.getTimeMillis(endTime)),
						"yyyy" + mContext.getString(R.string.ebei_unit_year) + "M" + mContext.getString(R.string.ebei_unit_month)
								+ "dd" + mContext.getString(R.string.ebei_unit_day));
			}
			mViewHolder.tvTime.setTextColor(mContext.getResources().getColor(R.color.ebei_text_common_light));
		}
		mViewHolder.tvTime.setText(time);

		AuctionPhase currentAuctionPhase = auction.getCurrentAuctionPhase();
		String currentPhaseId = currentAuctionPhase == null ? "" : currentAuctionPhase.getPhaseId();
		if (CommonConstants.STATE_BEGINNING.equals(auction.getStateCode())
				|| CommonConstants.STATE_FINISHED.equals(auction.getStateCode())) {
			List<AuctionPhase> auctionPhasesOld = auction.getAuctionPhases();
			// 只显示最新的3个
			List<AuctionPhase> auctionPhases = new ArrayList<AuctionPhase>();
			if (auctionPhasesOld.size() > 0) {
				int currentPhaseIndex = 0;
				if (CommonConstants.STATE_FINISHED.equals(auction.getStateCode())) {
					currentPhaseIndex = auctionPhasesOld.size() - 1;
				} else {
					if (currentAuctionPhase != null) {
						currentPhaseIndex = auctionPhasesOld.indexOf(currentAuctionPhase);
					}
				}

				if (currentPhaseIndex < 3) {
					auctionPhases = auctionPhasesOld;
				} else {
					for (int i = currentPhaseIndex; i > -1 && i > currentPhaseIndex - 3; i--) {
						auctionPhases.add(0, auctionPhasesOld.get(i));
					}
				}
			}

			if (auctionPhases.size() > 0) {
				AuctionPhase auctionPhase = auctionPhases.get(0);
				mViewHolder.tvColumnName1.setText(StringUtil.getDefaultString(auctionPhase.getPhaseName(), "-"));
				setColumnNameColor(mViewHolder.tvColumnName1, auctionPhase, currentPhaseId);
				mViewHolder.tvColumn11.setText(getPhaseContent(auctionPhase));
				setColumnColor(mViewHolder.tvColumn11, auctionPhase, currentPhaseId, false);
				mViewHolder.tvColumn21.setText(getPhaseOurContent(auctionPhase));
				setColumnColor(mViewHolder.tvColumn21, auctionPhase, currentPhaseId, true);
			} else {
				mViewHolder.tvColumnName1.setText("-");
				mViewHolder.tvColumn11.setText("-");
				mViewHolder.tvColumn21.setText("-");
				mViewHolder.tvColumnName1.setTextColor(mContext.getResources().getColor(R.color.ebei_text_common));
				mViewHolder.tvColumn11.setTextColor(mContext.getResources().getColor(R.color.ebei_text_common));
				mViewHolder.tvColumn21.setTextColor(mContext.getResources().getColor(R.color.ebei_text_common));
			}
			if (auctionPhases.size() > 1) {
				AuctionPhase auctionPhase = auctionPhases.get(1);
				mViewHolder.tvColumnName2.setText(StringUtil.getDefaultString(auctionPhase.getPhaseName(), "-"));
				setColumnNameColor(mViewHolder.tvColumnName2, auctionPhase, currentPhaseId);
				mViewHolder.tvColumn12.setText(getPhaseContent(auctionPhase));
				setColumnColor(mViewHolder.tvColumn12, auctionPhase, currentPhaseId, false);
				mViewHolder.tvColumn22.setText(getPhaseOurContent(auctionPhase));
				setColumnColor(mViewHolder.tvColumn22, auctionPhase, currentPhaseId, true);
			} else {
				mViewHolder.tvColumnName2.setText("-");
				mViewHolder.tvColumn12.setText("-");
				mViewHolder.tvColumn22.setText("-");
				mViewHolder.tvColumnName2.setTextColor(mContext.getResources().getColor(R.color.ebei_text_common));
				mViewHolder.tvColumn12.setTextColor(mContext.getResources().getColor(R.color.ebei_text_common));
				mViewHolder.tvColumn22.setTextColor(mContext.getResources().getColor(R.color.ebei_text_common));
			}
			if (auctionPhases.size() > 2) {
				AuctionPhase auctionPhase = auctionPhases.get(2);
				mViewHolder.tvColumnName3.setText(StringUtil.getDefaultString(auctionPhase.getPhaseName(), "-"));
				setColumnNameColor(mViewHolder.tvColumnName3, auctionPhase, currentPhaseId);
				mViewHolder.tvColumn13.setText(getPhaseContent(auctionPhase));
				setColumnColor(mViewHolder.tvColumn13, auctionPhase, currentPhaseId, false);
				mViewHolder.tvColumn23.setText(getPhaseOurContent(auctionPhase));
				setColumnColor(mViewHolder.tvColumn23, auctionPhase, currentPhaseId, true);
			} else {
				mViewHolder.tvColumnName3.setText("-");
				mViewHolder.tvColumn13.setText("-");
				mViewHolder.tvColumn23.setText("-");
				mViewHolder.tvColumnName3.setTextColor(mContext.getResources().getColor(R.color.ebei_text_common));
				mViewHolder.tvColumn13.setTextColor(mContext.getResources().getColor(R.color.ebei_text_common));
				mViewHolder.tvColumn23.setTextColor(mContext.getResources().getColor(R.color.ebei_text_common));
			}
		}

		int flagResId = -1;
		if (mViewHolder.tvTime.getText().toString().contains(mContext.getString(R.string.ebei_auction_doing))) {
			if (auction.getIsGiveup()) {
				flagResId = R.drawable.ebei_btn_flag_giveup;
			}
		}

		String bidderName = "";
		if (CommonConstants.STATE_FINISHED.equals(auction.getStateCode())) {
			if (auction.getIsGetted()) {
				flagResId = R.drawable.ebei_btn_flag_get;
			} else {
				flagResId = R.drawable.ebei_btn_flag_not_get;

				for (AuctionBidder auctionBidder : auction.getAuctionBidders()) {
					if (auctionBidder.getIsGetted()) {
						bidderName = StringUtil.getDefaultString(auctionBidder.getBidderAuctionCode()) + auctionBidder.getBidderName();
						bidderName = mContext.getString(R.string.ebei_get_company_item, bidderName);
						break;
					}
				}
				if (StringUtil.isEmpty(bidderName)) {
					bidderName= mContext.getString(R.string.ebei_not_company_get);
				}
			}
		}

		if (flagResId > -1) {
			mViewHolder.ivFlag.setImageResource(flagResId);
			mViewHolder.ivFlag.setVisibility(View.VISIBLE);
		} else {
			mViewHolder.ivFlag.setVisibility(View.GONE);
		}

		mViewHolder.viewAddress.setText(mLandInfo.getLocation());
		mViewHolder.viewAddress.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, AuctionDetailActivity.class);
				intent.putExtra(CommonConstants.AUCTION, auction);
				intent.putExtra("currentIndex", AuctionDetailActivity.TAB_LAND);
				mContext.startActivity(intent);
			}
		});

		if (CommonConstants.STATE_FINISHED.equals(auction.getStateCode()) && !auction.getIsGetted()) {
			mViewHolder.tvBidder.setText("(" + bidderName + ")");
			mViewHolder.tvBidder.setVisibility(View.VISIBLE);
		} else {
			mViewHolder.tvBidder.setVisibility(View.GONE);
		}

		return convertView;
	}

	public void setColumnNameColor(TextView textView, AuctionPhase auctionPhase, String currentPhaseId) {
		if (StringUtil.getDefaultString(auctionPhase.getPhaseId()).equals(currentPhaseId)) {
			textView.setTextColor(mContext.getResources().getColor(R.color.ebei_blue));
		} else {
			textView.setTextColor(mContext.getResources().getColor(R.color.ebei_text_common));
		}
	}

	public void setColumnColor(TextView textView, AuctionPhase auctionPhase, String currentPhaseId, boolean isMine) {
		String phaseResult = auctionPhase.getPhaseResult();
		if (StringUtil.isEmpty(phaseResult)) {
			textView.setTextColor(mContext.getResources().getColor(R.color.ebei_text_common_dark));
			return;
		}
		if (isMine) {
			phaseResult = auctionPhase.getPhaseOurResult();
		}
		if ((AuctionPhase.QUOTES_TYPE_ADD.equals(auctionPhase.getQuotesTypeCode()) && auctionPhase.compare(phaseResult, auctionPhase.getLimitPrice()) >= 0)
				|| (AuctionPhase.QUOTES_TYPE_MINUS.equals(auctionPhase.getQuotesTypeCode()) && auctionPhase.compare(phaseResult, auctionPhase.getLimitPrice()) <= 0)) {
			textView.setTextColor(mContext.getResources().getColor(R.color.ebei_red));
		} else {
			if (StringUtil.getDefaultString(auctionPhase.getPhaseId()).equals(currentPhaseId)) {
				textView.setTextColor(mContext.getResources().getColor(R.color.ebei_blue));
			} else {
				textView.setTextColor(mContext.getResources().getColor(R.color.ebei_text_common_dark));
			}
		}
	}

	public String getPhaseContent(AuctionPhase auctionPhase) {
		String result = "-";
		if (StringUtil.isEmpty(auctionPhase.getPhaseResult())) {
			return result;
		}
		result = auctionPhase.getPhaseResult() + auctionPhase.getUnit();
		result += auctionPhase.getPhaseResultRateString(auctionPhase.getPhaseResultRate());
		result = result + "\n" + (auctionPhase.getResultBidder() == null ? "" : (
				StringUtil.getDefaultString(auctionPhase.getResultBidder().getBidderAuctionCode()) + StringUtil.getDefaultString(auctionPhase.getResultBidder().getBidderName())));
		return result;
	}

	public String getPhaseOurContent(AuctionPhase auctionPhase) {
		String result = "-";
		if (StringUtil.isEmpty(auctionPhase.getPhaseOurResult())) {
			return result;
		}
		result = auctionPhase.getPhaseOurResult() + auctionPhase.getUnit();
		result += auctionPhase.getPhaseResultRateString(auctionPhase.getPhaseOurResultRate());
		return result;
	}

	public class ViewHolder {
		@BindView(R.id.tv_area)
		TextView tvArea;
		@BindView(R.id.tv_time)
		TextView tvTime;
		@BindView(R.id.iv_flag)
		ImageView ivFlag;
		@BindView(R.id.tvColumnName1)
		TextView tvColumnName1;
		@BindView(R.id.tvColumnName2)
		TextView tvColumnName2;
		@BindView(R.id.tvColumnName3)
		TextView tvColumnName3;
		@BindView(R.id.tvColumn11)
		TextView tvColumn11;
		@BindView(R.id.tvColumn12)
		TextView tvColumn12;
		@BindView(R.id.tvColumn13)
		TextView tvColumn13;
		@BindView(R.id.tvColumn21)
		TextView tvColumn21;
		@BindView(R.id.tvColumn22)
		TextView tvColumn22;
		@BindView(R.id.tvColumn23)
		TextView tvColumn23;
		@BindView(R.id.view_auction_info)
		LinearLayout viewAuctionInfo;
		@BindView(R.id.tv_bidder)
		TextView tvBidder;
		@BindView(R.id.view_address)
		TextView viewAddress;
		@BindView(R.id.view_title)
		View viewTitle;

		void setView(View view) {
			ButterKnife.bind(this, view);
		}
	}
}